<template>
  <div class='search'>
    <f-form ref='saleManform' :action='api.saleMan' :form-key='this.formItem.saleManId' handler-name='hySaleManSaveHandler'>
      <f-form-item label='唯一编码' name='saleManCode' :value='formItem.saleManCode' v-show='!isAdd'  />
      <f-form-item label='业务编码' name='saleManBusinessNo' :value='formItem.saleManBusinessNo' v-show='!isAdd' disabled />
      <f-form-item label='业务区域' name='areaShortName' :value='formItem.areaShortName' v-show='!isAdd' disabled />
      <f-form-item label='部门' placeholder='请选择部门' name='deptName' @click='openDeptModal({deptType:"ZY"})' :readOnly='true' :required='true' :value.sync='formItem.deptName' :trigger-change='true'>
        <a-tooltip slot='addonBefore' placement='top' title='点击图标选择部门'>
          <a-icon type='team' style='cursor:pointer' @click='openDeptModal({deptType:"ZY"})' />
        </a-tooltip>
      </f-form-item>
      <f-form-item label='姓名' name='saleManName' :value='formItem.saleManName' :required='true' />
      <f-form-item label='职务' ref='position' type='select' name='positionId' :value='formItem.positionId' :data='api.position.list' valueKey='positionId' label-key='positionName' :required='true' v-if='formItem.deptType=="ZY"' />
      <f-form-item label='身份证号' name='cardNo' :value='formItem.cardNo' required v-if='formItem.deptType=="ZY"'/>
      <f-form-item label='招募人' placeholder='请选择招募人' name='parentName' @click='openSaleManModal' :readOnly='true' :required='true' :value.sync='formItem.parentName' :trigger-change='true' v-if='formItem.deptType=="ZY"'>
        <a-tooltip slot='addonBefore' placement='top' title='点击图标选择招募人'>
          <a-icon type='user' style='cursor:pointer' @click='openSaleManModal' />
        </a-tooltip>
      </f-form-item>
      <f-form-item label='所属区域' type='areaSelect' :required='true' :value='areaIds' :level='2' name='areaIds' v-if='formItem.deptType=="ZY"'></f-form-item>
      <f-form-item label='联系电话' name='linkTel' :value='formItem.linkTel' />
      <f-form-item label='备注' name='remark' :value='formItem.remark' />
    </f-form>
    <select-dept-man-modal ref='saleManModal' @onSelectChange='onSelectSaleManChange' :selectType='3' title='招募人选择'></select-dept-man-modal>
    <select-dept-modal ref='deptModal' @onSelectChange='onSelectDeptChange'></select-dept-modal>
  </div>
</template>
<script>
import api from "../../../js/api"
import SelectDeptManModal from "@/views/module/haiying/components/select_deptman_modal"
import SelectDeptModal from "@/views/module/haiying/components/select_dept_modal"
import { commonMixins } from "@/core/mixins/commonMixins"

export default {
  name: "salemanForm",
  props: {},
  mixins: [commonMixins],
  watch: {
    "formItem.deptId"(newVal) {
      this.$nextTick(() => {
        //清除已选招募人
        this.$refs.saleManform.resetField("parentId")
        if (this.formItem.deptType == "ZY") {
          this.$refs.position.initData()
          
        }

      })
    }
  },
  data() {
    return {
      refs: this.$refs,
      areaIds: null,
      api: Object.assign(api, this.api),
      isAdd: true,//是否是添加模式
      autoLoad: false,
      loadSaleManParams: {
        saleManType: 1
      },
      loaded: false,//是否加载完成
      formItem: this.getFormItem(),
    }
  },
  methods: {
    getFormItem() {
      return {
        saleManId: "",
        saleManName: "",
        cardNo: null,
        parentId: null,
        deptId: null,
        positionOrder: null,
        positionId: null,
        linkTel: "",
        amount: 0,
        reputation: 0,
        reputationAmount: 0,
        bankNo: "",
        remark: "",
        bankName: ""
      }
    },
    //表单赋值
    setData(data) {
      this.formItem = data
      if (data && data.saleManId) {
        let params = {
          saleManId: data.saleManId
        }
        this.refs["saleManform"].getDetails(params, (data) => {
          this.formItem = data
          this.areaIds = [this.formItem.proId, this.formItem.cityId, this.formItem.areaId]
          this.loaded = true
        })
        this.isAdd = false
      } else {
        this.formItem = this.getFormItem()
        this.areaIds = []
        this.loaded = true
      }
    },
    /**
     * 选择招募人
     */
    openSaleManModal() {
      this.$refs["saleManModal"].open({
        deptId:this.formItem.deptId
      })
    },
    /**
     * 部门选择回调
     */
    onSelectDeptChange(selectedRowKeys, selectedRows) {
      this.$refs.saleManform.getForm().setFieldsValue({ "deptName": selectedRows[0].deptName })
      this.formItem.deptName = selectedRows[0].deptName
      this.formItem.deptId = selectedRows[0].deptId
      this.formItem.deptType = selectedRows[0].deptType
      this.$refs.saleManform.getForm().setFieldsValue({ "parentName": null })
      this.formItem.parentName = null
      this.formItem.parentId = null
      this.$forceUpdate()
    },
    /**
     * 业务员选择回调
     */
    onSelectSaleManChange(selectedRowKeys, selectedRows) {
      this.$refs.saleManform.getForm().setFieldsValue({ "parentName": selectedRows[0].saleManName })
      this.formItem.parentName = selectedRows[0].parentName
      this.formItem.parentId = selectedRowKeys[0]
      this.$forceUpdate()
    },
    //提交表单
    handleSubmit(callback) {
      if (this.formItem.deptType == "ZY") {
        if (!this.areaIds[0]) {
          this.$message.error("请选择业务区域")
          return
        }
      }
      this.$refs.saleManform.submit({
        deptId: this.formItem.deptId,
        parentId: this.formItem.parentId,
        orderNum: this.formItem.orderNum,
        proId: this.areaIds[0],
        cityId: this.areaIds[1],
        areaId: this.areaIds[2],
        saleManType: 1,
        saleManBusinessNo: this.formItem.saleManBusinessNo,
        companyId: 5
      }, callback)
    }
  },
  created() {

  },
  components: { SelectDeptManModal, SelectDeptModal }
}
</script>

<style scoped lang='less' rel='stylesheet/less'>

</style>
